<?php defined('SYSPATH') or die('No direct script access.');
/**
 *库存成本报表控制器
 *
 *@package Stockcost
 *@author taj
 *
 */

class Controller_Stockcost extends Controller_Admin{
/**
 *库存成本报表权限设置
 *
 */


/**
 *库存成本默认首界面
 *
 */
    public function action_index()
    {

    	try {
    		  ACL::Required('stockcost');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}


      $this->title = __('库存成本报表');

			$date_from = date('Y-m-01',time());
			$date_to = date('Y-m-d',time());
	
			$sum_num_cost = inventory::select_sum_num_cost($date_from,$date_to);//查询时间段里的所有库存成本		

			$view=View::factory('inventory/stockcost')
					->set('date_from',$date_from)
					->set('date_to',$date_to)
					->set('sum_num_cost',$sum_num_cost);

			$this->response->body($view);

	}
/**
 *通过条件查询库存成本结果
 *
 */
		public function action_selectstock()
		{
			set_time_limit(0);
			ini_set('memory_limit','2048M');
    	try {
    		  ACL::Required('stockcost');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}

    	    $this->title = __('库存成本报表');
			$date_from = isset($_GET['date_from'])?$_GET['date_from']:'';
			$date_to = isset($_GET['date_to'])?$_GET['date_to']:'';
            $sku = isset($_GET['sku'])?strtoupper($_GET['sku']):'';
            $day      = date('d',strtotime($date_from));

            $sum_num_cost = inventory::select_sum_num_cost($date_from,$date_to);//查询时间段里的所有库存成本

            if($sku!=''){
                $select=array();
                $inventory = inventory::select_sku_num_cost($sku,$date_from,$date_to);
                $select[0]['sku'] =$sku;
                $select[0]['num'] =$inventory['ending_num'];
                $select[0]['cost'] =$inventory['ending_cost'];
                $select[0]['open_cost'] =$inventory['opening_cost'];
                $select[0]['open_num'] =$inventory['opening_num'];
                $select[0]['in_cost'] =$inventory['wo_cost'];
                $select[0]['in_num'] =$inventory['wo_num'];
                $select[0]['out_num'] =$inventory['do_num'];
                $select[0]['out_cost'] =$inventory['do_cost'];
                $sku_val = DB::select('sku','sku_name','sku_standard','storage_position')
                    ->from('inventories')
                    ->where('sku','=',$sku)
                    ->execute('newerp')->as_array();
                $sku_arr[$sku]['sku_name'] =$sku_val[0]['sku_name'];
                $sku_arr[$sku]['sku_standard'] =$sku_val[0]['sku_standard'];
                $sku_arr[$sku]['storage_position'] =$sku_val[0]['storage_position'];
            }else{
            $stocks_num_sql = "SELECT UPPER(sku) as sku,sku_name,sku_standard,storage_position FROM  inventories WHERE 1 ";
            $stocks_num_sql .= " group by sku order by sku";
            $stocks_all = DB::query(Database::SELECT,$stocks_num_sql)->execute('newerp')->as_array();
            $stocks_num=count($stocks_all);
            $per_page = 5000;
            $stocks_arrs=array_chunk($stocks_all,$per_page);

      $pagination = Pagination::factory(array(
        'items_per_page' => $per_page,
        'total_items'    => $stocks_num,
	    'uri'	         => $this->request->uri(),
      ));
      $current_page     = isset($_GET['page'])?$_GET['page']:1;

                $db_sku = Arr::pluck($stocks_arrs[($current_page-1)],'sku');
                $vals_arr = Arr::unpack_string($db_sku,FALSE,"','");
                if($day=='01'){
                    $sql_select = "SELECT upper(sku) as sku,sum(num) as num,sum(cost) as cost,
                    sum(open_num) as open_num,sum(open_cost) as open_cost,
					sum(in_num) as in_num,sum(in_cost) as in_cost,
					sum(out_num) as out_num,sum(out_cost) as out_cost from (
				SELECT
					warehouseorders.sku AS sku,
					warehouseorders.warehouse_quantity AS num,
					warehouseorders.cost AS cost,
					0 as open_num,
					0 as open_cost,
					warehouseorders.warehouse_quantity AS in_num,
					warehouseorders.cost AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					warehouseorders
				WHERE
					warehouseorders.warehouse_date >= '".$date_from."'
				AND
					warehouseorders.warehouse_date <= '".$date_to." 23:59:59'
			    UNION ALL
				SELECT
					`deliveryorders`.`sku` AS `sku` ,
					- (`deliveryorders`.`quantity`) AS `num`,
					- (`deliveryorders`.`cost`) AS `cost`,
					0 as open_num,
					0 as open_cost,
					0 AS in_num,
					0 AS in_cost,
					`deliveryorders`.`quantity` AS out_num,
					`deliveryorders`.`cost` AS out_cost
				FROM
					`deliveryorders`
				WHERE
					`deliveryorders`.`delivery_date` >= '".$date_from."'
				AND
					`deliveryorders`.`delivery_date` < '".$date_to." 23:59:59'
		    	UNION ALL
				SELECT
					`inventories`.`sku` as `sku`,
					`inventories`.`quantity` AS `num`,
					`inventories`.`cost` as `cost`,
					`inventories`.`quantity` as open_num,
					`inventories`.`cost` as open_cost,
					0 AS in_num,
					0 AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					`inventories`
				WHERE
					`inventories`.`inventory_date` = '".$date_from."'
					) a where sku in ('".$vals_arr."') GROUP BY sku";
                    $select = DB::query(Database::SELECT,$sql_select)->execute('newerp')->as_array();
                }else{
                    $qimo_day = date('Y-m-01', strtotime($date_from));//期初值
                    $sql_select_open = "SELECT upper(sku) as sku,sum(num) as num,sum(cost) as cost,
                    sum(open_num) as open_num,sum(open_cost) as open_cost,
					sum(in_num) as in_num,sum(in_cost) as in_cost,
					sum(out_num) as out_num,sum(out_cost) as out_cost from (
				SELECT
					warehouseorders.sku AS sku,
					warehouseorders.warehouse_quantity AS num,
					warehouseorders.cost AS cost,
					0 as open_num,
					0 as open_cost,
					warehouseorders.warehouse_quantity AS in_num,
					warehouseorders.cost AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					warehouseorders
				WHERE
					warehouseorders.warehouse_date >= '".$qimo_day."'
				AND
					warehouseorders.warehouse_date <'".$date_from."'
			    UNION ALL
				SELECT
					`deliveryorders`.`sku` AS `sku` ,
					- (`deliveryorders`.`quantity`) AS `num`,
					- (`deliveryorders`.`cost`) AS `cost`,
					0 as open_num,
					0 as open_cost,
					0 AS in_num,
					0 AS in_cost,
					`deliveryorders`.`quantity` AS out_num,
					`deliveryorders`.`cost` AS out_cost
				FROM
					`deliveryorders`
				WHERE
					`deliveryorders`.`delivery_date` >= '".$qimo_day."'
				AND
					`deliveryorders`.`delivery_date` <'".$date_from."'
		    	UNION ALL
				SELECT
					`inventories`.`sku` as `sku`,
					`inventories`.`quantity` AS `num`,
					`inventories`.`cost` as `cost`,
					`inventories`.`quantity` as open_num,
					`inventories`.`cost` as open_cost,
					0 AS in_num,
					0 AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					`inventories`
				WHERE
					`inventories`.`inventory_date` = '".$qimo_day."'
					) a where sku in ('".$vals_arr."') GROUP BY sku order by sku";
                    $select_open = DB::query(Database::SELECT,$sql_select_open)->execute('newerp')->as_array();

                    //算非月初的库存成本
                    $sql_select= "SELECT upper(sku) as sku,
					sum(in_num) as in_num,sum(in_cost) as in_cost,
					sum(out_num) as out_num,sum(out_cost) as out_cost from (
				SELECT
					warehouseorders.sku AS sku,
					warehouseorders.warehouse_quantity AS in_num,
					warehouseorders.cost AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					warehouseorders
				WHERE
					warehouseorders.warehouse_date >= '".$date_from."'
				AND
					warehouseorders.warehouse_date <= '".$date_to." 23:59:59'
			    UNION ALL
				SELECT
					`deliveryorders`.`sku` AS `sku` ,
					0 AS in_num,
					0 AS in_cost,
					`deliveryorders`.`quantity` AS out_num,
					`deliveryorders`.`cost` AS out_cost
				FROM
					`deliveryorders`
				WHERE
					`deliveryorders`.`delivery_date` >= '".$date_from."'
				AND
					`deliveryorders`.`delivery_date` <'".$date_to." 23:59:59'
		    	UNION ALL
				SELECT
					`inventories`.`sku` as `sku`,
					0 AS in_num,
					0 AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					`inventories`
				WHERE
					`inventories`.`inventory_date` = '".$qimo_day."'
					) a where sku in ('".$vals_arr."') GROUP BY sku order by sku";
                    $select = DB::query(Database::SELECT,$sql_select)->execute('newerp')->as_array();
                   foreach($select as $v=>$val){
                       $select[$v]['open_num'] = $select_open[$v]['num'];
                       $select[$v]['open_cost'] = $select_open[$v]['cost'];
                       $select[$v]['num'] = $select_open[$v]['num']+$select[$v]['in_num']-$select[$v]['out_num'];
                       $select[$v]['cost'] = $select_open[$v]['cost']+$select[$v]['in_cost']-$select[$v]['out_cost'];
                   }

                }
                $sku_arr = array();
                foreach($stocks_arrs[($current_page-1)] as $value){
                    $sku_arr[$value['sku']]['sku_name'] =$value['sku_name'];
                    $sku_arr[$value['sku']]['sku_standard'] =$value['sku_standard'];
                    $sku_arr[$value['sku']]['storage_position'] =$value['storage_position'];
                }
            }
			$view=View::factory('inventory/stockcost')
					->set('stockcost',$select)
                    ->set('sku_arr',$sku_arr)
					->set('date_from',$date_from)
					->set('date_to',$date_to)
					->set('sku',$sku)
					->set('pagination',$pagination)
					->set('sum_num_cost',$sum_num_cost);

			$this->response->body($view);

		}

    /**
     *通过查询条件下载对应的库存成本结果
     *
     */
		public function action_down()
		{
				try {
    		  ACL::Required('stockcost down');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}
				set_time_limit(0);
				ini_set('memory_limit','1024M');
				require_once(Kohana::find_file('vendor', 'PHPExcel/PHPExcel/IOFactory'));
				$this->objReader  = PHPExcel_IOFactory::createReader('Excel5');
				$objPHPExcel      = new PHPExcel();
				$objPHPExcel->getProperties()->setCreator("ROMWE")
				->setLastModifiedBy("ROMWE")
				->setTitle("Office 2003 XLSX Test Document")
				->setSubject("Office 2003 XLSX Test Document")
				->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
				->setKeywords("office 2003 openxml php")
				->setCategory("Test result file");

			$date_from = isset($_GET['date_from'])?$_GET['date_from']:'';
			$date_to = isset($_GET['date_to'])?$_GET['date_to']:'';
			$sku = isset($_GET['sku'])?$_GET['sku']:'';

            $day      = date('d',strtotime($date_from));
            $qimo_day = date('Y-m-01', strtotime($date_from));//期初值

            if($sku!=''){
                $select=array();
                $k=0;
                $inventory = inventory::select_sku_num_cost($sku,$date_from,$date_to);
                $select[0]['sku'] =$sku;
                $select[0]['num'] =$inventory['ending_num'];
                $select[0]['cost'] =$inventory['ending_cost'];
                $select[0]['open_cost'] =$inventory['opening_cost'];
                $select[0]['open_num'] =$inventory['opening_num'];
                $select[0]['in_cost'] =$inventory['wo_cost'];
                $select[0]['in_num'] =$inventory['wo_num'];
                $select[0]['out_num'] =$inventory['do_num'];
                $select[0]['out_cost'] =$inventory['do_cost'];
                $sku_val = DB::select('sku','sku_name','sku_standard','storage_position')
                    ->from('inventories')
                    ->where('sku','=',$sku)
                    ->execute('newerp')->as_array();
                $sku_arr[$sku]['sku_name'] =$sku_val[0]['sku_name'];
                $sku_arr[$sku]['sku_standard'] =$sku_val[0]['sku_standard'];
                $sku_arr[$sku]['storage_position'] =$sku_val[0]['storage_position'];
                $objPHPExcel->createSheet($k);
                $objActSheet =$objPHPExcel->setActiveSheetIndex($k);
                $objActSheet->setCellValue('A1', 'sku');
                $objActSheet->setCellValue('B1', 'sku名称');
                $objActSheet->setCellValue('C1', 'sku规格');
                $objActSheet->setCellValue('D1', '储位');
                $objActSheet->setCellValue('E1', '期初数量');
                $objActSheet->setCellValue('F1', '期初成本');
                $objActSheet->setCellValue('G1', '本期入库数量');
                $objActSheet->setCellValue('H1', '本期入库成本');
                $objActSheet->setCellValue('I1', '本期出库数量');
                $objActSheet->setCellValue('J1', '本期出库成本');
                $objActSheet->setCellValue('K1', '期末数量');
                $objActSheet->setCellValue('L1', '期末成本');
                $i=2;
                foreach($select as $st){

                    $objActSheet->setCellValue('A'.$i, $st['sku']);
                    $objActSheet->setCellValue('B'.$i, $sku_arr[$st['sku']]['sku_name']);
                    $objActSheet->setCellValue('C'.$i, $sku_arr[$st['sku']]['sku_standard']);
                    $objActSheet->setCellValue('D'.$i, $sku_arr[$st['sku']]['storage_position']);

                    $objActSheet->setCellValue('E'.$i,$st['open_num']);
                    $objActSheet->setCellValue('F'.$i,$st['open_cost']);
                    $objActSheet->setCellValue('G'.$i,$st['in_num']);
                    $objActSheet->setCellValue('H'.$i,$st['in_cost']);
                    $objActSheet->setCellValue('I'.$i,$st['out_num']);
                    $objActSheet->setCellValue('J'.$i,$st['out_cost']);
                    $objActSheet->setCellValue('K'.$i,$st['num']);
                    $objActSheet->setCellValue('L'.$i,$st['cost']);
                    $i++;
                }
            }else{

            $stocks_num_sql = "SELECT UPPER(sku) as sku,sku_name,sku_standard,storage_position FROM  inventories WHERE 1 ";
            $stocks_num_sql .= " group by sku order by sku";
            $stocks_all = DB::query(Database::SELECT,$stocks_num_sql)->execute('newerp')->as_array();

            $stocks_arrs=array_chunk($stocks_all,5000);
			
			foreach($stocks_arrs as $k=>$vals){
                unset($db_sku);unset($vals_arr);unset($sql_select);unset($select);
                unset($sql_select_open);unset($select_open);unset($sku_arr);
                $db_sku = Arr::pluck($vals,'sku');
                $vals_arr = Arr::unpack_string($db_sku,FALSE,"','");
            if($day=='01'){
            $sql_select = "SELECT upper(sku) as sku,sum(num) as num,sum(cost) as cost,
                    sum(open_num) as open_num,sum(open_cost) as open_cost,
					sum(in_num) as in_num,sum(in_cost) as in_cost,
					sum(out_num) as out_num,sum(out_cost) as out_cost from (
				SELECT
					warehouseorders.sku AS sku,
					warehouseorders.warehouse_quantity AS num,
					warehouseorders.cost AS cost,
					0 as open_num,
					0 as open_cost,
					warehouseorders.warehouse_quantity AS in_num,
					warehouseorders.cost AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					warehouseorders
				WHERE
					warehouseorders.warehouse_date >= '".$date_from."'
				AND
					warehouseorders.warehouse_date <= '".$date_to." 23:59:59'
			    UNION ALL
				SELECT
					`deliveryorders`.`sku` AS `sku` ,
					- (`deliveryorders`.`quantity`) AS `num`,
					- (`deliveryorders`.`cost`) AS `cost`,
					0 as open_num,
					0 as open_cost,
					0 AS in_num,
					0 AS in_cost,
					`deliveryorders`.`quantity` AS out_num,
					`deliveryorders`.`cost` AS out_cost
				FROM
					`deliveryorders`
				WHERE
					`deliveryorders`.`delivery_date` >= '".$date_from."'
				AND
					`deliveryorders`.`delivery_date` <= '".$date_to." 23:59:59'
		    	UNION ALL
				SELECT
					`inventories`.`sku` as `sku`,
					`inventories`.`quantity` AS `num`,
					`inventories`.`cost` as `cost`,
					`inventories`.`quantity` as open_num,
					`inventories`.`cost` as open_cost,
					0 AS in_num,
					0 AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					`inventories`
				WHERE
					`inventories`.`inventory_date` = '".$date_from."'
					) a where sku in ('".$vals_arr."') GROUP BY sku";
                $select = DB::query(Database::SELECT,$sql_select)->execute('newerp')->as_array();

            }else{
                $qimo_day = date('Y-m-01', strtotime($date_from));//期初值
                $sql_select_open = "SELECT upper(sku) as sku,sum(num) as num,sum(cost) as cost,
                    sum(open_num) as open_num,sum(open_cost) as open_cost,
					sum(in_num) as in_num,sum(in_cost) as in_cost,
					sum(out_num) as out_num,sum(out_cost) as out_cost from (
				SELECT
					warehouseorders.sku AS sku,
					warehouseorders.warehouse_quantity AS num,
					warehouseorders.cost AS cost,
					0 as open_num,
					0 as open_cost,
					warehouseorders.warehouse_quantity AS in_num,
					warehouseorders.cost AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					warehouseorders
				WHERE
					warehouseorders.warehouse_date >= '".$qimo_day."'
				AND
					warehouseorders.warehouse_date <'".$date_from."'
			    UNION ALL
				SELECT
					`deliveryorders`.`sku` AS `sku` ,
					- (`deliveryorders`.`quantity`) AS `num`,
					- (`deliveryorders`.`cost`) AS `cost`,
					0 as open_num,
					0 as open_cost,
					0 AS in_num,
					0 AS in_cost,
					`deliveryorders`.`quantity` AS out_num,
					`deliveryorders`.`cost` AS out_cost
				FROM
					`deliveryorders`
				WHERE
					`deliveryorders`.`delivery_date` >= '".$qimo_day."'
				AND
					`deliveryorders`.`delivery_date` <'".$date_from."'
		    	UNION ALL
				SELECT
					`inventories`.`sku` as `sku`,
					`inventories`.`quantity` AS `num`,
					`inventories`.`cost` as `cost`,
					`inventories`.`quantity` as open_num,
					`inventories`.`cost` as open_cost,
					0 AS in_num,
					0 AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					`inventories`
				WHERE
					`inventories`.`inventory_date` = '".$qimo_day."'
					) a where sku in ('".$vals_arr."') GROUP BY sku order by sku";
                $select_open = DB::query(Database::SELECT,$sql_select_open)->execute('newerp')->as_array();

                //算非月初的库存成本
                $sql_select= "SELECT upper(sku) as sku,
					sum(in_num) as in_num,sum(in_cost) as in_cost,
					sum(out_num) as out_num,sum(out_cost) as out_cost from (
				SELECT
					warehouseorders.sku AS sku,
					warehouseorders.warehouse_quantity AS in_num,
					warehouseorders.cost AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					warehouseorders
				WHERE
					warehouseorders.warehouse_date >= '".$date_from."'
				AND
					warehouseorders.warehouse_date <= '".$date_to." 23:59:59'
			    UNION ALL
				SELECT
					`deliveryorders`.`sku` AS `sku` ,
					0 AS in_num,
					0 AS in_cost,
					`deliveryorders`.`quantity` AS out_num,
					`deliveryorders`.`cost` AS out_cost
				FROM
					`deliveryorders`
				WHERE
					`deliveryorders`.`delivery_date` >= '".$date_from."'
				AND
					`deliveryorders`.`delivery_date` <'".$date_to." 23:59:59'
		    	UNION ALL
				SELECT
					`inventories`.`sku` as `sku`,
					0 AS in_num,
					0 AS in_cost,
					0 AS out_num,
					0 AS out_cost
				FROM
					`inventories`
				WHERE
					`inventories`.`inventory_date` = '".$qimo_day."'
					) a where sku in ('".$vals_arr."') GROUP BY sku order by sku";
                $select = DB::query(Database::SELECT,$sql_select)->execute('newerp')->as_array();
                foreach($select as $v=>$val){
                    $select[$v]['open_num'] = $select_open[$v]['num'];
                    $select[$v]['open_cost'] = $select_open[$v]['cost'];
                    $select[$v]['num'] = $select_open[$v]['num']+$select[$v]['in_num']-$select[$v]['out_num'];
                    $select[$v]['cost'] = $select_open[$v]['cost']+$select[$v]['in_cost']-$select[$v]['out_cost'];
                }

            }
                $sku_arr = array();
                foreach($vals as $value){
                    $sku_arr[$value['sku']]['sku_name'] =$value['sku_name'];
                    $sku_arr[$value['sku']]['sku_standard'] =$value['sku_standard'];
                    $sku_arr[$value['sku']]['storage_position'] =$value['storage_position'];
                }


    		$objPHPExcel->createSheet($k);
				$objActSheet =$objPHPExcel->setActiveSheetIndex($k);
				$objActSheet->setCellValue('A1', 'sku');
				$objActSheet->setCellValue('B1', 'sku名称');
				$objActSheet->setCellValue('C1', 'sku规格');
				$objActSheet->setCellValue('D1', '储位');
				$objActSheet->setCellValue('E1', '期初数量');
				$objActSheet->setCellValue('F1', '期初成本');
				$objActSheet->setCellValue('G1', '本期入库数量');
				$objActSheet->setCellValue('H1', '本期入库成本');
				$objActSheet->setCellValue('I1', '本期出库数量');
				$objActSheet->setCellValue('J1', '本期出库成本');
				$objActSheet->setCellValue('K1', '期末数量');
				$objActSheet->setCellValue('L1', '期末成本');
            $i=2;
			foreach($select as $st){

				$objActSheet->setCellValue('A'.$i, $st['sku']);
				$objActSheet->setCellValue('B'.$i, $sku_arr[$st['sku']]['sku_name']);
				$objActSheet->setCellValue('C'.$i, $sku_arr[$st['sku']]['sku_standard']);
				$objActSheet->setCellValue('D'.$i, $sku_arr[$st['sku']]['storage_position']);

					$objActSheet->setCellValue('E'.$i,$st['open_num']);
					$objActSheet->setCellValue('F'.$i,$st['open_cost']);
					$objActSheet->setCellValue('G'.$i,$st['in_num']);
					$objActSheet->setCellValue('H'.$i,$st['in_cost']);
					$objActSheet->setCellValue('I'.$i,$st['out_num']);
					$objActSheet->setCellValue('J'.$i,$st['out_cost']);
					$objActSheet->setCellValue('K'.$i,$st['num']);
					$objActSheet->setCellValue('L'.$i,$st['cost']);
                $i++;
			}

            }
            }
				$objPHPExcel->getActiveSheet($k)->setTitle('库存成本');
				$objPHPExcel->setActiveSheetIndex($k);
				unset($select);unset($sql_select);unset($sku_arr);

				$day=date("Y-m-d");
            ob_end_clean();
				header("Content-Type: application/vnd.ms-excel; charset=utf-8");
				header('Content-Disposition: attachment;filename="'.$day.'库存成本.xls"');
				header('Cache-Control: max-age=0');
				$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
				$objWriter->save('php://output');
				exit;

		}
//暂未使用，下载csv格式的数据
		public function action_down_csv(){
				set_time_limit(0);
				ini_set('memory_limit','1024M');

			$day=date("Y-m-d").'库存成本';
            ob_end_clean();
			header("Content-type: application/vnd.ms-excel;charset=UTF-8");
		 	header("Content-disposition: attachment; filename=$day.csv");
	 		header ("Cache-Control: no-cache, must-revalidate");
	 		header ("Pragma: no-cache");


			$date_from = isset($_GET['date_from'])?$_GET['date_from']:'';
			$date_to = isset($_GET['date_to'])?$_GET['date_to']:'';
			$sku = isset($_GET['sku'])?$_GET['sku']:'';

			$stocks = "SELECT `inventory_date`,`sku`,`sku_name`,`sku_standard`,`storage_position` FROM `inventories` WHERE 1 ";
			if($sku!=''){
				$stocks .= " AND sku='".$sku."'";
			}
		
      $stocks    .="  group by sku order by sku limit 30000";
      $stocks1 = DB::query(Database::SELECT, $stocks)->execute('newerp')->as_array();

			$content = '';
			$content .='sku,sku_name,sku_big,sku_place,opening_num,opening_cost,current_in_num,current_in_cost,current_out_num,current_out_cost,ending_num,ending_cost'.chr(13).chr(10);
			foreach($stocks1 as $st){

				$inventory = new Service_Inventory();
				$kucun = $inventory->balances($date_from,$date_to,$st['sku']);

				if((max($kucun['opening'][$st['sku']])+max($kucun['current_in'][$st['sku']])+max($kucun['current_out'][$st['sku']])+max($kucun['closing'][$st['sku']]))==0){continue;unset($kucun);}

				$content .=$st['sku'].',';
				$content .=$st['sku_name'].',';				
				$content .=$st['sku_standard'].',';		
				$content .=$st['storage_position'].',';		
				$content .=','.chr(13).chr(10);


			}

		echo $content;
		die();
		}
}
